package com.icss.OA.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.icss.OA.common.R;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FileController {

       //文件放在项目的images下
        private static final String FILE_PATH_LOCAL =  "D:\\Images\\";
        private static final String PREFIXC =  "\\Images\\";
        @PostMapping(value = "upload")
        @ResponseBody
        public R upload(@RequestParam("file") MultipartFile file,HttpServletRequest request) {
          
             //file.isEmpty(); 判断图片是否为空
             //file.getSize(); 图片大小进行判断
             
             String name = request.getParameter("name");
             System.out.println("用户名："+name);
            
             // 获取文件名
            String fileName = file.getOriginalFilename();            
            System.out.println("上传的文件名为：" + fileName);
            
            // 获取文件的后缀名,比如图片的jpeg,png
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            System.out.println("上传的后缀名为：" + suffixName);
            
            // 文件上传后的路径
            fileName = UUID.randomUUID() + suffixName;
            System.out.println("转换后的名称:"+fileName);
            
            File dest = new File(FILE_PATH_LOCAL + fileName);
           
            try {
                file.transferTo(dest);
                //上传成功
                return R.success(fileName);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
              //上传失败
            return   R.success(fileName);
        }

    @GetMapping(value = "deleteImage")
    @ResponseBody
    public R upload(@RequestParam("img") String name) {
        File f = new File(FILE_PATH_LOCAL, name);

        boolean b = f.delete();

        if (b) {
            return  R.success("删除成功");
        } else {
            return   R.fail("删除失败重洗设置");
        }
    }
}